package com.ctrl.riveradministrator.mvp.view.activity.trace.daemon;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.ctrl.riveradministrator.R;


public class DaemonManager {
    private static DaemonManager instance;
    private Context mContext;
    private Activity mKeepLiveActivity;
    private WindowManager.LayoutParams params;
    private WindowManager windowManager;
    private ConstraintLayout layout;
    private int statusBarHeight = -1;

    public DaemonManager(Context context) {
        this.mContext = context;
    }

    public static DaemonManager getInstance(Context context) {
        if (instance == null) {
            instance = new DaemonManager(
                    context.getApplicationContext());
        }
        return instance;
    }

    public void setKeepLiveActivity(Activity keepliveActivity) {
        this.mKeepLiveActivity = keepliveActivity;
    }

    public void finishKeepLiveActivity() {
        if (mKeepLiveActivity != null)
            mKeepLiveActivity.finish();
    }

    public void startKeepLiveActivity() {
        Intent intent = new Intent(mContext, MicroPixelActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    /**
     * 创建1像素悬浮窗，供亮屏时使用
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createFloatView(Context context) {
        params = new WindowManager.LayoutParams();//初始化params
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;//设置type，用于其他程序窗口之上
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {//6.0+
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        params.format = PixelFormat.RGBA_8888;//设置效果半透明
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//不可聚焦及不可使用按钮对悬浮窗进行操控

        //初始化窗口停靠位置
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = 0;
        params.y = 0;

        //窗口大小
        params.width = 1;
        params.height = 1;

        LayoutInflater inflater = LayoutInflater.from(context);
        layout = (ConstraintLayout) inflater.inflate(R.layout.float_view, null); //获取浮动窗口视图所在布局.
        windowManager.addView(layout, params);//添加layout

        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); //主动计算出当前View的宽高信息.

        //用于检测状态栏高度.
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }

    }
}
